package com.xijia.wuyouwallet.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.xijia.wuyouwallet.R;

import cn.pedant.SweetAlert.SweetAlertDialog;


public abstract class BaseFragment extends Fragment {
    protected SweetAlertDialog progress;
    //Fragment当前状态是否可见
    protected boolean isVisible;
    //protected SweetAlertDialog progress;

    //protected SweetAlertDialog isCancelProgress;
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    //可见
    protected void onVisible() {
        lazyLoad();
    }

    //不可见
    protected void onInvisible() {

    }

    //延迟加载
    //子类必须重写此方法
    protected abstract void lazyLoad();



    /**
     * General method for starting a new activity either for result or not.
     *
     * @param activityClass The activity to start
     * @param bundle        Extra information with this intent.
     * @param isReturn      If start for result or not
     * @param requestCode   The request code.
     * @param isFinish      If finish self after start.
     */
    public void jumpToPage(Class<?> activityClass, Bundle bundle, boolean isReturn, int requestCode, boolean isFinish) {
        if (activityClass == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(getActivity(), activityClass);

        if (bundle != null) {
            intent.putExtras(bundle);
        }

        if (isReturn) {
            startActivityForResult(intent, requestCode);
        } else {
            startActivity(intent);
        }

        if (isFinish) {
            //            finish();
        }
    }

    public void jumpToPage(Class<?> activityClass) {
        jumpToPage(activityClass, null, false, 0, false);
    }

    public void jumpToPage(Class<?> activityClass, boolean isFinish) {
        jumpToPage(activityClass, null, false, 0, isFinish);
    }

    public void jumpToPage(Class<?> activityClass, Bundle bundle) {
        jumpToPage(activityClass, bundle, false, 0, false);
    }

    public void jumpToPage(Class<?> activityClass, Bundle bundle, boolean isFinish) {
        jumpToPage(activityClass, bundle, false, 0, isFinish);
    }

    public void jumpToPage(Class<?> activityClass, boolean isReturn, int requestCode) {
        jumpToPage(activityClass, null, isReturn, requestCode, false);
    }

    public void jumpToPage(Class<?> activityClass, boolean isReturn, Bundle bundle, int requestCode) {
        jumpToPage(activityClass, bundle, isReturn, requestCode, false);
    }

    //弹出吐司
    public void showToast(final String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }
    /**
     * 显示进度条
     */
    protected void showProgressDialog(final boolean isCancel) {
                if (progress == null) {
                    progress = new SweetAlertDialog(getActivity(), SweetAlertDialog.PROGRESS_TYPE);
                    progress.getProgressHelper().setBarColor(getResources().getColor(R.color.main_orange));
                    progress.setTitleText("加载中...");
                    if (isCancel){
                        progress.setCancelable(true);
                    }else {
                        progress.setCancelable(false);
                    }
                }
                progress.show();

    }

    /**
     * 隐藏进度条
     */
    protected void dismissProgressDialog() {
                if (progress != null) {
                    progress.dismiss();
                }
    }
}
